newemp" />
python3:set 和 frozenset的应用场景及区别 | 您所在的位置:网站首页 › python3 函数解包应用场景 › python3:set 和 frozenset的应用场景及区别 |
set 是集合,frozenset 是冻结的集合,顾名思义是不可变集合。 set 最大的特性是不重合,在去重的时候用的最多。 1.接受一个可迭代的类型先简单的看下class 的说明如下: class set(object): """ set() -> new empty set object set(iterable) -> new set object Build an unordered collection of unique elements.set(iterable)它接受一个可迭代的类型,所以我们就大胆的用吧 可以传入字符串,list ,dict ,tuple a=set("abcde") print(a) b=set([1,2,3]) print(b) c=set({"a":1,"b":2}) print(c) d=set((1,2)) print(d)打印结果: {'b', 'd', 'a', 'e', 'c'} {1, 2, 3} {'b', 'a'} {1, 2} 2.无序性和dict区别从上边的打印结果 {‘b’, ‘d’, ‘a’, ‘e’, ‘c’}可以证明是无序性。 大家注意下,他和dict 极为相似,但是却属于set类型,自己分辨下。大家都知道 dict语法是{key:value,key:value} 是这个格式,下边用type 打印出看看是什么类型? a={"a","b","c"} print(type(a))打印结果: 3.set和frozenset 的区别刚开始也讲了frozenset是不可变的,如果修改是报错的,那到底有什么用处呢 应用场景:一般dict 的key 是不变的,我们就可以用 那我们用代码证明下,set不会报错,frozenset 会报错. 先改变set类型 a={"a","b","c"} a.add("d") print((a))打印结果: {'c', 'a', 'd', 'b'}修改frozenset会报错 如下: a=frozenset("abcd") print(a) a.add("d") print((a))打印结果: frozenset({'b', 'c', 'a', 'd'}) AttributeError: 'frozenset' object has no attribute 'add' 4.set方法介绍set 的方法非常多, 我截取源码一些方法,然后挑几个讲解下 def clear(self, *args, **kwargs): # real signature unknown def copy(self, *args, **kwargs): # real signature unknown def difference(self, *args, **kwargs): # real signature unknown """ Return the difference of two or more sets as a new set. def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ pass def discard(self, *args, **kwargs): # real signature unknown """ Remove an element from a set if it is a member. If the element is not a member, do nothing. """ pass def intersection(self, *args, **kwargs): # real signature unknown """ Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.) """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ pass def pop(self, *args, **kwargs): # real signature unknown def remove(self, *args, **kwargs): # real signature unknown def union(self, *args, **kwargs): # real signature unknown """ Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. """ pass1. update 方法 源码里的描述很清晰( “”" Update a set with the union of itself and others. “”") 意思就是联合两个set,取的是总集。 看例子: a=set("abc") a.update("bcd") print(a)打印结果: {'b', 'a', 'c', 'd'}把两个set 集合在一起并去重。 2. difference 的用法 先看方法描述吧 ( Return the difference of two or more sets as a new set.) 用例子加深印象。 a=set("abc") print(a.difference("bcd")) print(a)打印结果: {'a'} {'c', 'a', 'b'}调用方法之后直接返回一个不同的元素。 看第二个输出他不会对原set 进行改变。 其他的方法自己应用下,应该都不难。 5. set集合运算(|,&,-)看实例吧 a=set("abc") b=set("bcd") print(a-b) #对a进行去重 print(a | b) #并集 print(a & b) #交集打印结果: {'a'} {'d', 'c', 'b', 'a'} {'c', 'b'}之所以可以这么操作,原理他是调用了相应的魔法函数。 例如 : “-” 调用的是 def __sub__(self, *args, **kwargs): # real signature unknown """ Return self-value. """ pass“|” 调用的是 def __or__(self, *args, **kwargs): # real signature unknown """ Return self|value. """ pass“&”调用的是 def __and__(self, *args, **kwargs): # real signature unknown """ Return self&value. """ pass总结: frozenset和set 区别是可变不可变,可接受可迭代的参数. set的性能比较高 因为他用到了哈希编码. set 的集合运算也要看下,非常重要. |
CopyRight 2018-2019 实验室设备网 版权所有 |